package com.xz.group12306.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xz.group12306.business.common.Md5Utils;
import com.xz.group12306.business.service.impl.UserServiceImpl;
import com.xz.group12306.domain.User;

/**
 * @author 胡晨倩
 * 登录
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//String target = "";
		//修改乱码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
        //获取页面的值
		String username = request.getParameter("username");
		
		String password =Md5Utils.md5( request.getParameter("password"));
		String rule = request.getParameter("rule");
		String vvcode = request.getParameter("vvcode");		
		String vcode =(String)request.getSession().getAttribute("code");

		UserServiceImpl userServiceImpl=new UserServiceImpl();
		//User user=userServiceImpl.login(username, Md5Utils.md5(password), rule);
		User user=userServiceImpl.login(username,password, rule);
	//	User user2=userServiceImpl.showAll(username, password);
		System.out.println(user);
		if (vvcode.equals(vcode)) {
			if (user != null) {
				//request.getSession().setAttribute("user", user);
				request.getSession().setAttribute("user", user);
				if ("1".equals(rule)) {
					//1是管理员
					//target = "user/login.html";
					HttpSession session=request.getSession();
					session.setAttribute("username", username);
			        response.sendRedirect("admin/AdminIndex.jsp");
					//response.getWriter().print("<script>window.location.href='admin/AdminIndex.jsp';</script>");
				} else if ("2".equals(rule)) {
					//2是用户
					//target = "user/index.html";
					HttpSession session=request.getSession();
					session.setAttribute("username", username);
			        response.sendRedirect("user/index.jsp");
					//response.getWriter().print("<script>window.location.href='user/index.html';</script>");
				    
				}
			} if (user == null) {
				response.getWriter().print("<script>alert(\"用户名密码输入错误,请重新登录\");window.location.href='user/login.html';</script>");
				//target = "user/login.html";
			}
		} else {
			//target = "user/login.html";
			response.getWriter().print("<script>alert(\"验证码输入错误,请重新登录\");window.location.href='user/login.html';</script>");
		}
		/*RequestDispatcher dispatcher=request.getRequestDispatcher(target);
		dispatcher.forward(request, response);*/
		
		
		
	}

}
